package com.example.smart_city_0829.activity.digitallibrary;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.digitalLibrary.LibraryComment;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 评论点赞条目复用问题
 */
public class LibraryCommentActivity extends BaseActivity {

    private EditText commentEt;
    private Button sendBt;
    private MaxSizeLv lv;
    private Button loadMore;

    private CommentListAdapter adapter;


    private int id = -1;
    private int pageNum = 1;
    private final int pageSize = 10;

    private List<LibraryComment.DataBean> comments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_library_comment;
    }

    @Override
    protected void initView() {
        commentEt = (EditText) findViewById(R.id.comment_et);
        sendBt = (Button) findViewById(R.id.send_bt);
        lv = (MaxSizeLv) findViewById(R.id.lv);
        loadMore = (Button) findViewById(R.id.load_more);

    }

    @Override
    protected void initData() {
        id = getIntent().getIntExtra("id", -1);

        getComment();
    }

    @Override
    protected void initEvent() {
        loadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++pageNum;
                getComment();
            }
        });
        sendBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter == null) {
                    showToastShort("正在加载中，请稍等...");
                    return;
                }
                final String comment = commentEt.getText().toString();
                final HashMap<String, Object> params = new HashMap<>();
                params.put("libraryId", id);
                params.put("content", comment);
                api.sendLibraryComment(
                                RequestBody.create(gson.toJson(params), MediaType.parse("application/json")))
                        .enqueue(new NetCallBack<Base>() {
                            @Override
                            public void onResponse(Call<Base> call, Response<Base> response) {
                                showToastShort(response.body().getMsg());
                                if (response.body().getMsg().contains("成功")) {
                                    final LibraryComment.DataBean dataBean = new LibraryComment.DataBean();
                                    dataBean.setContent(comment);
                                    dataBean.setUserName(sharedPreferences.getString("nickName", ""));
                                    dataBean.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                    comments.add(dataBean);
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        });
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "详细评论";
    }

    private void getComment() {
        api.getLibraryComment(
                        id,
                        pageNum,
                        pageSize)
                .enqueue(new NetCallBack<LibraryComment>() {
                    @Override
                    public void onResponse(Call<LibraryComment> call, Response<LibraryComment> response) {
                        comments.addAll(response.body().getData());
                        comments = comments.stream().sorted(Comparator.comparing(LibraryComment.DataBean::getLikeCount).reversed())
                                .collect(Collectors.toList());
                        if (adapter == null) {
                            adapter = new CommentListAdapter(comments, LibraryCommentActivity.this);
                            lv.setAdapter(adapter);
                        } else {
                            adapter.resetData(comments);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private final class CommentListAdapter extends CommonAdapter<LibraryComment.DataBean, CommonAdapter.CommentListViewHolder> {


        public CommentListAdapter(List<LibraryComment.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LibraryComment.DataBean data, int position, CommentListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.name.setText(String.valueOf(data.getUserName()));
            viewHolder.content.setText(String.valueOf(data.getContent()));
            viewHolder.tag1.setText(String.valueOf(data.getCreateTime()));
            viewHolder.cb.setChecked(data.isMyLikeState());

            viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    data.setMyLikeState(isChecked);
                    if (isChecked) {
                        data.setLikeCount(data.getLikeCount() + 1);
                    } else {
                        data.setLikeCount(data.getLikeCount() - 1);
                    }
                    comments = comments.stream().sorted(Comparator.comparing(LibraryComment.DataBean::getLikeCount).reversed())
                            .collect(Collectors.toList());
                    adapter.resetData(comments);
                    adapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        protected CommentListViewHolder getViewHolder() {
            return new CommentListViewHolder();
        }

        @Override
        protected void initView(CommentListViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.comment_list_item;
        }
    }

}
